import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import "../styles" as Styles

Rectangle {
    id: dataGridHeader

    // 自定义属性
    property string text: ""
    property bool sortable: false
    property string sortColumn: ""
    property string currentSortColumn: ""
    property bool sortAscending: true
    property alias textAlignment: headerText.horizontalAlignment

    // 信号
    signal sort(string column, bool ascending)

    color: "transparent"

    RowLayout {
        anchors.fill: parent
        anchors.leftMargin: Styles.Theme.spacingSmall
        anchors.rightMargin: Styles.Theme.spacingSmall
        spacing: Styles.Theme.spacingXSmall

        Text {
            id: headerText
            text: dataGridHeader.text
            color: Styles.Theme.textPrimary
            font.pixelSize: Styles.Theme.fontSizeSmall
            font.weight: Styles.Theme.fontWeightMedium
            Layout.fillWidth: true
            elide: Text.ElideRight
            horizontalAlignment: Text.AlignLeft
        }

        // 排序指示器
        Text {
            visible: sortable && currentSortColumn === sortColumn
            text: sortAscending ? "▲" : "▼"
            color: Styles.Theme.primary
            font.pixelSize: 10
        }
    }

    MouseArea {
        anchors.fill: parent
        enabled: sortable
        cursorShape: sortable ? Qt.PointingHandCursor : Qt.ArrowCursor
        onClicked: {
            if (sortable) {
                var newAscending = currentSortColumn === sortColumn ? !sortAscending : true
                sort(sortColumn, newAscending)
            }
        }
    }

    // 悬停效果
    Rectangle {
        anchors.fill: parent
        color: Styles.Theme.primary
        opacity: 0
        visible: sortable

        PropertyAnimation {
            id: hoverAnimation
            target: parent
            property: "opacity"
            duration: 150
            easing.type: Easing.OutCubic
        }
    }

    MouseArea {
        anchors.fill: parent
        enabled: sortable
        hoverEnabled: true
        onEntered: {
            hoverAnimation.to = 0.1
            hoverAnimation.start()
        }
        onExited: {
            hoverAnimation.to = 0
            hoverAnimation.start()
        }
        onClicked: {
            if (sortable) {
                var newAscending = currentSortColumn === sortColumn ? !sortAscending : true
                sort(sortColumn, newAscending)
            }
        }
    }
}